package com.pingancar.creditmanage.rest;

import com.pingancar.creditmanage.action.auth.ShopAuthPassport;
import com.pingancar.creditmanage.dao.OrderitemDaoImpl;
import com.pingancar.creditmanage.pojo.OrderitemPojo;
import com.pingancar.creditmanage.pojo.ShopinfoPojo;
import com.pingancar.creditmanage.rest.model.BaseResult;
import com.pingancar.creditmanage.service.ShopServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by SilverStone-WW on 2015/2/16.
 */
@RestController
@RequestMapping("/rest/shop/")
@ShopAuthPassport
public class ShopController {
    @Autowired
    private ShopServiceImpl shopService;

    @RequestMapping("orderlist")
    public BaseResult getOrderlist(Integer offset, Integer length, HttpSession session) {
        BaseResult baseResult = new BaseResult();
        baseResult.setStatus(-1);
        ShopinfoPojo shopinfoPojo = (ShopinfoPojo)session.getAttribute("shop");

        Integer[] status = {OrderitemPojo.CONFIRM, OrderitemPojo.COMMENT};
        List list = shopService.getOrderlistByShop(shopinfoPojo, offset, length, status);
        baseResult.setData(list);
        baseResult.setStatus(0);

        return baseResult;
    }

    @RequestMapping("cash")
    public BaseResult cash (HttpSession session) {
        BaseResult baseResult = new BaseResult();
        ShopinfoPojo shopinfoPojo = (ShopinfoPojo)session.getAttribute("shop");
        String message = shopService.cash(shopinfoPojo);
        if (message.equals("success")) {
            shopinfoPojo.setCredit(0);
            session.setAttribute("shop", shopinfoPojo);
            baseResult.setStatus(0);
            baseResult.setInfo("申请成功，请等待确认");
        } else {
            baseResult.setStatus(-1);
            baseResult.setInfo(message);
        }
        return baseResult;
    }
}
